unit uLancamentosContasPagar;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.DBCtrls, Vcl.Mask, Vcl.Buttons,
  Vcl.ExtCtrls, Vcl.Grids, Vcl.DBGrids, Vcl.StdCtrls, JvExStdCtrls, JvButton,
  JvCtrls, JvEdit, JvValidateEdit, Vcl.ComCtrls, Data.DB, JvExMask, JvToolEdit,
  JvDBControls;

type
  TfrmLancamentosContasPagar = class(TForm)
    PageControlLancamentos: TPageControl;
    TabSheet2: TTabSheet;
    GroupBox1: TGroupBox;
    Label10: TLabel;
    edtTapdescr: TEdit;
    btnFiltrar: TJvImgBtn;
    GroupBox2: TGroupBox;
    DBGridLancamentos: TDBGrid;
    Panel1: TPanel;
    DBNavigator: TDBNavigator;
    btnNovo: TJvImgBtn;
    btnEditar: TJvImgBtn;
    btnExcluir: TJvImgBtn;
    TabSheet1: TTabSheet;
    Label29: TLabel;
    Panel2: TPanel;
    btnOk: TBitBtn;
    btnCancelar: TBitBtn;
    TimerInicio: TTimer;
    dsTbltap: TDataSource;
    GroupBox6: TGroupBox;
    Label21: TLabel;
    Label22: TLabel;
    Label23: TLabel;
    DBEdit9: TDBEdit;
    DBEdit10: TDBEdit;
    DBEdit11: TDBEdit;
    Label1: TLabel;
    DBEdit1: TDBEdit;
    Label4: TLabel;
    DBEdit6: TDBEdit;
    Label6: TLabel;
    Label7: TLabel;
    DBEdit8: TDBEdit;
    Label9: TLabel;
    DBLookupComboBox1: TDBLookupComboBox;
    dsTblfdp: TDataSource;
    Label3: TLabel;
    JvDBDateEdit1: TJvDBDateEdit;
    JvDBDateEdit2: TJvDBDateEdit;
    Label28: TLabel;
    Label2: TLabel;
    Label8: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    rgSituacao: TRadioGroup;
    rgPeriodo: TRadioGroup;
    Label5: TLabel;
    edtDataIni: TJvDateEdit;
    Label11: TLabel;
    edtDataFim: TJvDateEdit;
    btnBaixa: TJvImgBtn;
    procedure TimerInicioTimer(Sender: TObject);
    procedure btnFiltrarClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure btnNovoClick(Sender: TObject);
    procedure btnOkClick(Sender: TObject);
    procedure btnCancelarClick(Sender: TObject);
    procedure btnEditarClick(Sender: TObject);
    procedure btnExcluirClick(Sender: TObject);
    procedure TabSheet1Show(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure rgPeriodoClick(Sender: TObject);
    procedure btnBaixaClick(Sender: TObject);
  private
    { Private declarations }
    procedure FiltrarLancamentos;
    procedure ControlarPageControl(APageIndex: integer);

  public
    { Public declarations }
  end;

var
  frmLancamentosContasPagar: TfrmLancamentosContasPagar;

implementation

{$R *.dfm}

uses uDadosContasPagar, uPesquisaCliFor, uDadosFornecedores, uBaixaContasPagar;

{ TfrmLancamentosContasPagar }

procedure TfrmLancamentosContasPagar.btnBaixaClick(Sender: TObject);
begin
  frmBaixaContasPagar := TfrmBaixaContasPagar.Create(self);
  try
    dmDadosContasPagar.EditarBaixaLancamento;

    if frmBaixaContasPagar.ShowModal = mrOk then
      FiltrarLancamentos;
  finally
    FreeAndNil(frmBaixaContasPagar);
  end;
end;

procedure TfrmLancamentosContasPagar.btnCancelarClick(Sender: TObject);
begin
  dmDadosContasPagar.CalcelarLancamento;

  ControlarPageControl(0);
end;

procedure TfrmLancamentosContasPagar.btnEditarClick(Sender: TObject);
begin
  dmDadosContasPagar.EditarLancamento;

  ControlarPageControl(1);
end;

procedure TfrmLancamentosContasPagar.btnExcluirClick(Sender: TObject);
begin
  try
    dmDadosContasPagar.ExcluirLancamento;
  except
    on E: Exception do
    begin
      MessageDlg(e.Message, mtError,[mbOK],0);
      FiltrarLancamentos;
    end;
  end;
end;

procedure TfrmLancamentosContasPagar.btnFiltrarClick(Sender: TObject);
begin
  FiltrarLancamentos;
end;

procedure TfrmLancamentosContasPagar.btnNovoClick(Sender: TObject);
begin
  dmDadosContasPagar.AdicionarLancamento;

  ControlarPageControl(1);
end;

procedure TfrmLancamentosContasPagar.btnOkClick(Sender: TObject);
begin
  dmDadosContasPagar.SalvarLancamento;

  FiltrarLancamentos;

  ControlarPageControl(0);
end;

procedure TfrmLancamentosContasPagar.ControlarPageControl(APageIndex: integer);
begin
  if APageIndex = 0 then
  begin
    PageControlLancamentos.Pages[0].TabVisible := true;  // Tab de consulta
    PageControlLancamentos.Pages[1].TabVisible := false; // Tab de manutencao
    PageControlLancamentos.ActivePageIndex := 0;
  end
  else
  begin
    PageControlLancamentos.Pages[0].TabVisible := false; // Tab de consulta
    PageControlLancamentos.Pages[1].TabVisible := true;  // Tab de manutencao
    PageControlLancamentos.ActivePageIndex := 1;
  end;
end;

procedure TfrmLancamentosContasPagar.FiltrarLancamentos;
var
  SQL: String;
  SQLOrder: String;
begin
  SQLOrder := ' order by tapdtvenc ';

  if rgSituacao.ItemIndex = 0 then
    SQL := SQL + ' and tapdtpagto is not null '
  else if rgSituacao.ItemIndex = 1 then
    SQL := SQL + ' and tapdtpagto is null ';

  if (trim(edtDataIni.Text) <> '/  /') and (trim(edtDataFim.Text) <> '/  /') then
  begin
    if rgPeriodo.ItemIndex = 0 then
    begin
      SQL := SQL + ' and tapdtemis between "' + FormatDateTime('yyyy-mm-dd', edtDataIni.Date) + '" and "' + FormatDateTime('yyyy-mm-dd', edtDataFim.Date) + '" ';
      SQLOrder := ' order by tapdtemis ';
    end
    else if rgPeriodo.ItemIndex = 1 then
    begin
      SQL := SQL + ' and tapdtvenc between "' + FormatDateTime('yyyy-mm-dd', edtDataIni.Date) + '" and "' + FormatDateTime('yyyy-mm-dd', edtDataFim.Date) + '" ';
      SQLOrder := ' order by tapdtvenc ';
    end
    else
    begin
      SQL := SQL + ' and tapdtpagto between "' + FormatDateTime('yyyy-mm-dd', edtDataIni.Date) + '" and "' + FormatDateTime('yyyy-mm-dd', edtDataFim.Date) + '" ';
      SQLOrder := ' order by tapdtpagto ';
    end;
  end;

  if Trim(edtTapdescr.Text) <> EmptyStr then
    SQL := SQL + ' and tapdescr like "%' + trim(edtTapdescr.Text) + '%" ';

  SQL :=
    'select *      ' +
    '  from tbltap ' +
    ' where 1 = 1  ' +
    SQL +
    SQLOrder;

  dmDadosContasPagar.FiltrarLancamentos(SQL);


end;

procedure TfrmLancamentosContasPagar.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action := caFree;
  Release;
  frmLancamentosContasPagar := nil;
end;

procedure TfrmLancamentosContasPagar.FormKeyPress(Sender: TObject;
  var Key: Char);
begin
  if key = #13 Then
  begin
    key := #0;
    Perform(Wm_NextDlgCtl,0,0);
  end;
end;

procedure TfrmLancamentosContasPagar.rgPeriodoClick(Sender: TObject);
begin
  edtDataIni.SetFocus;
end;

procedure TfrmLancamentosContasPagar.TabSheet1Show(Sender: TObject);
begin
  DBEdit1.SetFocus;
end;

procedure TfrmLancamentosContasPagar.TimerInicioTimer(Sender: TObject);
begin
  TimerInicio.Enabled := false;

  ControlarPageControl(0);

  frmLancamentosContasPagar.WindowState := wsMaximized;

  FiltrarLancamentos;
end;

end.
